#!/bin/bash
current_path=$(
    cd $(dirname $0)
    pwd
)

osc_path=$1
user_name=$2
user_email=$3
rpm_version=$4

function prepare_tools() {
    echo "[INFO]====================prepare tools=========================="
    mkdir -p ${current_path}/auto_tools
    rm -rf ${current_path}/auto_tools/*
    cd ${current_path}/auto_tools
    git clone https://gitee.com/lhwerico/auto_tool.git
    sed -i "s/name='wangchong'/name='${user_name}'/" ${current_path}/auto_tools/auto_tool/spec_related/spec_prep_plus.sh
    sed -i "s/email='wangchong56@huawei.com'/email='${user_email}'/" ${current_path}/auto_tools/auto_tool/spec_related/spec_prep_plus.sh

    # git clone https://gitee.com/zhu-yuncheng/python-script
    # git clone https://gitee.com/wangchong1995924/lao_wang_9527
    # sed -i "s/name='wangchong'/name='${user_name}'/" ${current_path}/auto_tools/lao_wang_9527/script/spec_related/spec_prep_plus.sh
    # sed -i "s/email='wangchong56@huawei.com'/email='${user_email}'/" ${current_path}/auto_tools/lao_wang_9527/script/spec_related/spec_prep_plus.sh
    # git clone https://gitee.com/src-openeuler/CheckSpecByPyrpm
    # git clone https://gitee.com/Lostwayzxc/rpm_check
}

function download_src_rpm() {
    echo "[INFO]====================download src rpm=========================="
    cd ${current_path}/auto_tools/auto_tool/src_import
    if [[ -n ${rpm_version} ]]; then
        python3 ./fedora_project.py ${current_path}/rpm_list.txt ${osc_path} ${rpm_version}
    else
        python3 ./fedora_project.py ${current_path}/rpm_list.txt ${osc_path}
    fi
}

function build_old_rpm() {
    rpm_name=$1
    echo "[INFO]====================build old src rpm （${rpm_name}）=========================="
    mkdir -p ${current_path}/build_rpm/tmp
    cd ${osc_path}/${rpm_name}
    echo "[INFO] build process view ${current_path}/buildold_${rpm_name}.txt"
    osc build --no-verify --root=${current_path}/build_rpm/tmp >${current_path}/buildold_${rpm_name}.txt
    if [[ $? -ne 0 ]]; then
        echo "[ERROR] osc build old rpm ${rpm_name} failed"
        echo "!!! [INFO] [INFO] [INFO] Error message view ${current_path}/buildold_${rpm_name}.txt [INFO] [INFO] [INFO]!!!"
        return 1
    else
        echo "[INFO] osc build old rpm ${rpm_name} succeed"
    fi
    mkdir -p ${current_path}/build_rpm/old
    rm -rf ${current_path}/build_rpm/old/*
    cp ${current_path}/build_rpm/tmp/home/abuild/rpmbuild/RPMS/aarch64/* ${current_path}/build_rpm/old >/dev/null 2>&1
    cp ${current_path}/build_rpm/tmp/home/abuild/rpmbuild/RPMS/i386/* ${current_path}/build_rpm/old >/dev/null 2>&1
    cp ${current_path}/build_rpm/tmp/home/abuild/rpmbuild/RPMS/noarch/* ${current_path}/build_rpm/old >/dev/null 2>&1 
}

function update_and_check() {
    rpm_name=$1
    echo "[INFO]====================update and check spec file (${rpm_name})=========================="
    cd ${current_path}/auto_tools/auto_tool/check_spec
    python3 CheckSpec.py -p ${osc_path}/${rpm_name} --specshell ${current_path}/auto_tools/auto_tool/spec_related/spec_prep_plus.sh --specify
    #echo "!!! [INFO] [INFO] [INFO] Error message view ${current_path}/auto_tools/CheckSpecByPyrpm/packages_errorLog.txt [INFO] [INFO] [INFO]!!!"
}

function build_new_rpm() {
    rpm_name=$1
    echo "[INFO]====================build new rpm (${rpm_name})=========================="
    cd ${osc_path}/${rpm_name}
    if [[ -f ./${rpm_name}.spec~ ]]; then
        mv ./${rpm_name}.spec~ ./${rpm_name}.specfc
        old_rpm_release=$(cat ./${rpm_name}.specfc | grep "Release:" | awk -F '%' '{print $1}' | awk -F ' ' '{print $2}')
        new_rpm_release=$((${old_rpm_release} + 1))
        echo "[INFO] new rpm release is ${new_rpm_release}"
        #sed -i "s/Release:    1/Release:    ${new_rpm_release}/" ./${rpm_name}.spec
        echo "[INFO] build process view ${current_path}/buildnew_${rpm_name}.txt"
        osc build --no-verify --release=${new_rpm_release} --root=${current_path}/build_rpm/tmp >${current_path}/buildnew_${rpm_name}.txt

        if [[ $? -ne 0 ]]; then
            echo "[ERROR] osc build new rpm ${rpm_name} failed"
            echo "!!! [INFO] [INFO] [INFO] Error message view ${current_path}/buildnew_${rpm_name}.txt [INFO] [INFO] [INFO]!!!"
            return 1
        else
            echo "[INFO] osc build new rpm ${rpm_name} succeed"
        fi
    fi
    mkdir -p ${current_path}/build_rpm/new
    rm -rf ${current_path}/build_rpm/new/*
    cp ${current_path}/build_rpm/tmp/home/abuild/rpmbuild/RPMS/aarch64/* ${current_path}/build_rpm/new >/dev/null 2>&1
    cp ${current_path}/build_rpm/tmp/home/abuild/rpmbuild/RPMS/i386/* ${current_path}/build_rpm/new >/dev/null 2>&1
    cp ${current_path}/build_rpm/tmp/home/abuild/rpmbuild/RPMS/noarch/* ${current_path}/build_rpm/new >/dev/null 2>&1
}

function rpm_check() {
    echo "[INFO]====================check old and new rpm=========================="
    cd ${current_path}/auto_tools/auto_tool/rpm_check
    python3 verify.py -p1 ${current_path}/build_rpm/new -p2 ${current_path}/build_rpm/old
    #echo "!!! [INFO] [INFO] [INFO] Error message view ${current_path}/auto_tools/rpm_check/Log.txt [INFO] [INFO] [INFO]!!!"
}

function create_yaml() {
    rpm_name=$1
    echo "[INFO]===================create yaml (${rpm_name})=============================="
    cd ${osc_path}/${rpm_name}
    touch ${rpm_name}.yaml
    echo "version: " >>${rpm_name}.yaml
    echo "src_repo: " >>${rpm_name}.yaml
    echo "tag_prefix: " >>${rpm_name}.yaml
    echo "separator: " >>${rpm_name}.yaml
}

function commit_new_rpm() {
    rpm_name=$1
    echo "[INFO]====================commit rpm (${rpm_name})=========================="
    cd ${osc_path}/${rpm_name}
    #tmp_rpm_release=$(cat ./${rpm_name}.spec | grep "Release:")
    #sed -i "s/${tmp_rpm_release}/Release:    1/" ./${rpm_name}.spec
    osc add *
    osc commit -m "update"
}

function main() {
    echo -n "The package has exist on obs?
    1 - Exist(no need download)
    2 - Not Exist(need download)
    ?:"
    read arg

    prepare_tools
    if [[ "X${arg}" == "X2" ]]; then
        download_src_rpm
    fi
    import_rpms_file=${current_path}/rpm_list.txt
    for rpm_name in $(cat ${import_rpms_file}); do
        cd ${osc_path}
        osc co ${rpm_name} >/dev/null 2>&1
        if [[ -d ${osc_path}/${rpm_name} ]]; then
            build_old_rpm ${rpm_name}
            if [[ $? -ne 0 ]]; then
                continue
            fi
            update_and_check ${rpm_name}
            build_new_rpm ${rpm_name}
            if [[ $? -ne 0 ]]; then
                continue
            fi
            rpm_check
            create_yaml ${rpm_name}
            commit_new_rpm ${rpm_name}
        fi
    done
}

main
